package util;

import org.apache.poi.util.StringUtil;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

/**
 * ClassName: RedisTemplate
 * Package: util
 * Description:
 *
 * @Author LIEX
 * @create 2024/7/15 18:58
 * @Version 1.0
 */
public class RedisUtil {
    private final static ShardedJedisPool redisPool;
    static {
        Properties prop = new Properties();
        InputStream in = null;
        String host;
        String port;
        String password;
        try {
            in=MorphiaUtil.class.getResourceAsStream("/redis.properties");
            prop.load(in);
            host = prop.getProperty("host");
            port = prop.getProperty("port");
            password = prop.getProperty("password");
        }catch (Exception e){
            throw new RuntimeException("初始化 Redis 客户端配置文件异常",e);
        }
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(100);
        config.setMaxIdle(50);
        config.setMaxWaitMillis(3000);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        JedisShardInfo jedisShardInfo1 = new JedisShardInfo(host, Integer.parseInt(port));
        if(StringUtil.isNotBlank(password)){
            jedisShardInfo1.setPassword(password);
        }
        List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
        list.add(jedisShardInfo1);
        redisPool = new ShardedJedisPool(config, list);
    }
    public static ShardedJedis  getJedis(){
        return redisPool.getResource();
    }
}
